﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Net.Sockets;
using System.Net;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using ClassLibrary;
using ClassLibrary.Configuration.Switch;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace NetworkManager_Test
{
    public partial class Form1 : Form
    {
        private Socket client;
        private byte[] data = new byte[1024];
        private int size = 1024;

        byte[] m_DataBuffer = new byte[1000];
        IAsyncResult m_asynResult;
        public AsyncCallback pfnCallBack;



        public Form1()
        {
            InitializeComponent();

        }


     //connect
        private void button1_Click(object sender, EventArgs e)
        {
            Conect("127.0.0.1", 9050);
           
        }

        // --- socket methods
        // create the socket...
        public void Conect(String strIpAddress, int portNo)
        {
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // get the remote IP address...
            var ipAddress = IPAddress.Parse(strIpAddress);
            //create the end point
            var ipEndPoint = new IPEndPoint(ipAddress.Address, portNo);
            //connect to the remote host...
            client.Connect(ipEndPoint); 
                string stringData = "A";
               byte[] message1 = Encoding.ASCII.GetBytes(stringData);
               client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
                         new AsyncCallback(SendData), client);
               String a=client.LocalEndPoint.ToString();
               
               
            //watch for data ( asynchronously )...
            WaitForData();
        }

        void SendData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int sent = remote.EndSend(iar);
            remote.BeginReceive(data, 0, size, SocketFlags.None,
                          new AsyncCallback(OnDataReceived), remote);
        }

        private void WaitForData()
        {
            if (pfnCallBack == null)
                pfnCallBack = new AsyncCallback(OnDataReceived);
            // now start to listen for any data...
            m_asynResult =
                client.BeginReceive(m_DataBuffer, 0, m_DataBuffer.Length, SocketFlags.None, pfnCallBack, null);
        }

        private void OnDataReceived(IAsyncResult asyn)
        {
            //end receive...
            var bytesReceivedCount = client.EndReceive(asyn);
            var binaryFormatter = new BinaryFormatter();
            var memoryStream = new MemoryStream(m_DataBuffer, 0, bytesReceivedCount);
            var Config = (ConfSwitchingTable.Record)binaryFormatter.Deserialize(memoryStream);

      
            WaitForData();
        }


    }
}
